#include <iostream>
#include <string>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <exception>
#include <fstream>
#include <boost/algorithm/string.hpp>
#include <vector>

using namespace boost;
using namespace std;


#include "templateprocess.h"


int main(int argc, char* argv[])
{
	int matched_num = 0;
	const char* Usage = "Usage:\r\n"
	"This program <template file> <test file>\r\n"
	"e.g. libTemplateProcTest template_list.txt testfile >testfile.result";
	if (argc < 2)
	{
		std::cout << Usage << std::endl;
		exit(-1);
	}
	try
	{
		using namespace std;
		UserCommand user_cmd;
		TemplateProc template_processor;
		template_processor.Init(argv[1]);
		UserCommandInfo user_cmd_info;
		memset(&user_cmd_info, 0, sizeof(UserCommandInfo));
		std::ifstream test_fp(argv[2], ios_base::in);
		if (test_fp.is_open())
		{
			std::string command_line;
			while (std::getline(test_fp, command_line, '\n'))
			{
				if (!command_line.empty())
				{
					std::vector<std::string> fields;
					boost::split(fields, command_line, boost::is_any_of("\t "), token_compress_off);
					strcpy(user_cmd.user_id_, fields[0].c_str());
					strcpy(user_cmd.sentence_, fields[1].c_str());
					strcpy(user_cmd_info.user_id_, user_cmd.user_id_);
					matched_num = template_processor.Process(&user_cmd, &user_cmd_info);
					memset(&user_cmd_info, 0, sizeof(UserCommandInfo));

				}
			}
		}
		test_fp.close();
		template_processor.Stop();
	}
	catch (const std::exception* e)
	{
		std::cout << e->what() << std::endl;
	}
	
	return 0;
}